<?php
/**
 * @package qBase
 * @subpackage base
 */

/**
 * The basic delegate object
 * i recommend not to extend this object and trigger functions or methods 
 * on the default pattern : $object->callback_method(QEvent $event, $delegate_parameters);
 * where $delegate_parameters is a mixed parameter
 * 
 * @package qBase
 * @subpackage base
 */
class QDelegate extends QObject 
{
	/**
	 * The object that is called if null the callback is to a function
	 *
	 * @var Object
	 */
	protected $callback_object;
	/**
	 * The callback method, may be a simple function of a function within an object
	 *
	 * @var string
	 */
	protected $callback_method;
	/**
	 * A delegate may have it's own parameters different of the event's parameters
	 * this may be usefull in many situations
	 *
	 * @var mixed
	 */
	protected $parameters;
		
	/**
	 * The constructor for QDelegate
	 *
	 * @param Object $callback_object
	 * @param string $callback_method
	 * @param mixed $parameters
	 */
	public function __construct($callback_object, $callback_method, $parameters = null)
	{
		$this->callback_object = $callback_object;
		$this->callback_method = $callback_method;
		$this->parameters = $parameters;
	}
	
	/**
	 * Executes the delegate usually on event request
	 *
	 * @param QEvent $event the event
	 */
	final public function execute(QEvent $event)
	{
		// var_dump($this->callback_object);
		if ($this->callback_object)
		{
			if ($this->parameters)
			{
				// eval("\$this->callback_object->{$this->callback_method}(\$event, \$this->parameters);");
				call_user_func_array(array($this->callback_object, $this->callback_method), array($event, $this->parameters));
			}
			else 
			{
				// eval("\$this->callback_object->{$this->callback_method}(\$event);");
				call_user_func_array(array($this->callback_object, $this->callback_method), array($event));
			}
		}
		else 
		{
			if ($this->parameters)
				call_user_func_array($this->callback_method, array($event, $this->parameters));
			else 
				call_user_func_array($this->callback_method, array($event));
		}
	}
}


?>